\NeedsTeXFormat{LaTeX2e}
\ProvidesPackage{stb-linguistics}[2019/04/09]

\RequirePackage{enumitem} % For making 'examples' environment
\RequirePackage{eqparbox}
\RequirePackage{etoolbox}
\RequirePackage[normalem]{ulem}
\RequirePackage{xparse}
\RequirePackage{xstring}

% --- Line up all leaves ---
\DeclareOption{leafline}{%
    \@ifpackageloaded{forest}{
        \forestset{
            default preamble={
                where n children=0{
                    tier=word,
                }{},
            },
        }
    }{}
}

% --- Italicize leaves ---
\DeclareOption{leafit}{%
    \@ifpackageloaded{forest}{
        \forestset{
            default preamble={
                where n children=0{
                    font=\itshape
                }{},
            }
        }
    }{}
}

% --- Bar/prime for X-bar theory ---
\newcommand*{\xbar}[1]{\ensuremath{\overline{\text{#1}}}}
\DeclareOption{xbarbar}{%
    \renewcommand*{\xbar}[1]{\ensuremath{\overline{\text{#1}}}}
}
\DeclareOption{xbarprime}{%
    \renewcommand*{\xbar}[1]{#1′}
}

% --- Make footnote examples continuously numbered ---
\newif\if@exfootnoteresume
\@exfootnoteresumefalse
\DeclareOption{exfootnoteresume}{\@exfootnoteresumetrue}

% --- Pass options to stb-brackets ---
\DeclareOption{setbar}{
    \PassOptionsToPackage{\CurrentOption}{stb-brackets}
}
\DeclareOption{setcolon}{
    \PassOptionsToPackage{\CurrentOption}{stb-brackets}
}
\DeclareOption{evalbar}{
    \PassOptionsToPackage{\CurrentOption}{stb-brackets}
}
\DeclareOption{evalbrackets}{
    \PassOptionsToPackage{\CurrentOption}{stb-brackets}
}

\ProcessOptions\relax

\RequirePackage{stb-brackets} % Load after options are passed

% --- Tree style ---
\@ifpackageloaded{forest}{
    \forestset{
        squish/.append style={for tree={s sep=0}},
    }
}{}

% --- Numbered examples environment ---
% Examples in main text
\newcounter{@texamples}
\newlist{texamples}{enumerate}{2}
\setlist[texamples]{labelsep=0.25em}
\setlist[texamples,1]{label=(\arabic*), before=\setcounter{\@exenv i}{\value{@\@exenv}}, after=\setcounter{@\@exenv}{\value{\@exenv i}}}
\setlist[texamples,2]{label=\alph*., ref=(\arabic{texamplesi}\alph*)}

% Examples in footnotes
\newcounter{@fexamples}
\newlist{fexamples}{enumerate}{2}
\setlist[fexamples]{labelsep=0.25em}
\setlist[fexamples,1]{label=(\roman*)}
\setlist[fexamples,2]{label=\alph*., ref=(\roman{fexamplesi}\alph*)}
\if@exfootnoteresume
    \setlist*[fexamples,1]{before=\setcounter{\@exenv i}{\value{@\@exenv}}, after=\setcounter{@\@exenv}{\value{\@exenv i}}}
\fi

\newcommand*{\customizeexamples}[2][1,2]{%
    \setlist*[texamples,#1]{#2}
    \setlist*[fexamples,#1]{#2}
}

% Get appropriate examples environment based one whether we're in a footnote
\newif\if@infootnote
\@infootnotefalse
\pretocmd{\@makefntext}{\@infootnotetrue}{}{}
\apptocmd{\@makefntext}{\@infootnotefalse}{}{}

\newcommand*{\@exenv}{\if@infootnote f\else t\fi examples}
\newenvironment{examples}[1][]{\begin{\@exenv}[#1]}{\end{\@exenv}}

% Custom \item for examples environments
\providecommand*{\phantomsection}{} % Fallback if hyperref isn't being used
\NewDocumentCommand{\ex}{O{}}{%
    \item[%
        \phantomsection%
        \stepcounter{\@enumctr}%
        \@itemlabel%
        { \eqmakebox[\@exenv-{\the\@listdepth}][r]{#1}}%
    ]%
    \protected@edef\@currentlabel{\@nameuse{the\@enumctr}}%
    \IfStrEq{\@exenv}{texamples}{%
        \label{\@exenv @\arabic{\@exenv i}-\ifnumequal{\@listdepth}{2}{\arabic{\@exenv ii}}{0}}%
    }{}%
}

% Relative cross-references to examples
\NewDocumentCommand{\exref}{m O{0}}{%
    \IfStrEq{\@exenv}{texamples}%
        {\ref{\@exenv @\the\numexpr \value{\@exenv i} + #1 \relax-#2}}%
        {\PackageError{stb-linguistics}{\protect\exref\space can't be used in footnotes (\@exenv)}}%
}

\NewDocumentCommand{\nextexref}{o}{\exref{1}[#1]} % (use \NewDocumentCommand for the these instead of \newcommand* so that -NoValue- is passed if there's no optional argument)
\NewDocumentCommand{\nnextexref}{o}{\exref{2}[#1]}
\NewDocumentCommand{\nnnextexref}{o}{\exref{3}[#1]}
\NewDocumentCommand{\nnnnextexref}{o}{\exref{4}[#1]}
\NewDocumentCommand{\nnnnnextexref}{o}{\exref{5}[#1]}

\NewDocumentCommand{\prevexref}{o}{\exref{0}[#1]}
\NewDocumentCommand{\pprevexref}{o}{\exref{-1}[#1]}
\NewDocumentCommand{\ppprevexref}{o}{\exref{-2}[#1]}
\NewDocumentCommand{\pppprevexref}{o}{\exref{-3}[#1]}
\NewDocumentCommand{\ppppprevexref}{o}{\exref{-4}[#1]}

% --- Glosses ---
\newcommand*{\@maxglosscols}{50}
\newcommand*{\gloss}[3]{%
    \begin{tabular}[t]{@{}*{\@maxglosscols}{l@{ }}}
        #1 \\
        #2 \\
        \multicolumn{\@maxglosscols}{@{}l}{#3}
    \end{tabular}%
}

% --- Split text across lines ---
\NewDocumentCommand{\declare@delimitedlines}{m m}{
    \NewDocumentCommand{#1}{O{} O{c} +m}{\ensuremath{#2*{\!\!\!\begin{tabular}[##1]{##2} ##3 \end{tabular}\!\!\!}}}
}
\declare@delimitedlines{\parenslines}{\parens}
\declare@delimitedlines{\bracketslines}{\brackets}
\declare@delimitedlines{\braceslines}{\braces}

% --- Semantic types ---
% Type constructors
\newcommand*{\T}[1]{\ensuremath{#1}}
\newcommand*{\fT}[2]{\ensuremath{\angles*{#1, #2}}}
\NewDocumentCommand{\newfT}{m m m}{%
    \NewDocumentCommand{#1}{s}{\IfBooleanTF{##1}{#2 #3}{\fT{#2}{#3}}}
}

% Type variables
\newcommand*{\Ttau}{\T{τ}}
\newcommand*{\Tsigma}{\T{σ}}
\newfT{\Ttausigma}{\Ttau}{\Tsigma}

% Atomic types
\newcommand*{\Td}{\T{d}}
\newcommand*{\Te}{\T{e}}
\newcommand*{\Ti}{\T{i}}
\newcommand*{\Ts}{\T{s}}
\newcommand*{\Tt}{\T{t}}
\newcommand*{\Tv}{\T{v}}

% Common function types
\newfT{\Tdt}{\Td}{\Tt}
\newfT{\Ted}{\Te}{\Td}
\newfT{\Tee}{\Te}{\Te}
\newfT{\Tet}{\Te}{\Tt}
\newfT{\Tst}{\Ts}{\Tt}
\newfT{\Ttt}{\Tt}{\Tt}

\newfT{\Tdet}{\Td}{\Tet*}
\newfT{\Teet}{\Te}{\Tet*}
\newfT{\Tete}{\Tet*}{\Te}
\newfT{\Tett}{\Tet*}{\Tt}
\newfT{\Tiet}{\Ti}{\Tet*}
\newfT{\Tttt}{\Tt}{\Ttt*}

\newfT{\Teeet}{\Te}{\Teet*}
\newfT{\Tetet}{\Tet*}{\Tet}

\newfT{\Tetett}{\Tet*}{\Tett*}

% --- Denotation brackets ---
\NewDocumentCommand{\deno}{s O{} m}{%
    \IfBooleanTF{#1}%
        {\ensuremath{\dbrackets*{\text{#3}}}}%
        {\ensuremath{\dbrackets[#2]{\text{#3}}}}%
}

% --- Labeled brackets ---
\NewDocumentCommand{\lbrackets}{o m o}{%
    [\IfValueT{#1}{\textsubscript{#1}~}%
    #2%
    \IfValueT{#3}{~\textsubscript{#3}}]%
}

% --- Sub-/superscript shortcuts ---
\providecommand*{\sub}[1]{\textsubscript{#1}}
\providecommand*{\super}[1]{\textsuperscript{#1}}

% --- Miscellaneous linguistics stuff ---
% Elision
\newcommand*{\elide}[1]{\sout{#1}}

% Blank
\newcommand*{\blank}[1]{\uline{\hspace{2em}}}

% Indices
\newcommand*{\ind}[1]{\textsubscript{\(#1\)}}
\newcommand*{\indt}[1]{\(t\)\ind{#1}}
\newcommand*{\ti}{\indt{i}}
\newcommand*{\tj}{\indt{j}}
\newcommand*{\tk}{\indt{k}}

% Symbols for glosses
\newcommand*{\acc}{\textsc{acc}}
\newcommand*{\dat}{\textsc{dat}}
\newcommand*{\erg}{\textsc{erg}}
\newcommand*{\gen}{\textsc{gen}}
\newcommand*{\inst}{\textsc{inst}}
\newcommand*{\loc}{\textsc{loc}}
\newcommand*{\nom}{\textsc{nom}}

\newcommand*{\sg}{\textsc{sg}}
\newcommand*{\pl}{\textsc{pl}}

\newcommand*{\first}{\textsc{1p}}
\newcommand*{\second}{\textsc{2p}}
\newcommand*{\third}{\textsc{3p}}

\newcommand*{\past}{\textsc{past}}
\newcommand*{\pres}{\textsc{pres}}
\newcommand*{\fut}{\textsc{fut}}
\newcommand*{\nonpast}{\textsc{nonpast}}
\newcommand*{\nonpres}{\textsc{nonpres}}

\newcommand*{\poss}{\textsc{poss}}

\endinput